package com.wwwarehouse.usercenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.wwwarehouse.common.activity.base.BaseActivity;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.Stack;

import de.greenrobot.event.EventBus;

/**
 * Created by Jackie on 2017/8/18.
 * 注册界面框架
 */


@Route(path = UserCenterConstant.PATH_REGISTER_CARD_ACTIVITY)
public class RegisterCardActivity extends BaseActivity implements View.OnClickListener {
    private TextView mFgTitleText;
    private ImageView mFgBackBtn;

    private Stack<Fragment> mBackStack;
    private RelativeLayout  mRlTitle;
    private StateButton     mBtSubmit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_register_card);

        initView();
        initEvent();
    }

    public void hideBack(){
        mFgBackBtn.setVisibility(View.INVISIBLE);
    }

    public void showBack(){
        mFgBackBtn.setVisibility(View.VISIBLE);
    }
    private void initView() {
        mFgTitleText = findView(R.id.fragment_title_text);
        mFgBackBtn = findView(R.id.fragment_back_bt);

        mRlTitle = findView(R.id.rl_title);
        mBtSubmit = findView(R.id.bt_submit);
        mBtSubmit.setOnClickListener(this);

        mBackStack = new Stack();
    }

    private void initEvent() {
        mFgBackBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == mFgBackBtn) {
            if (mBackStack.size() > 0) {
                popFragment();
            } else {
                EventBus.getDefault().post("back");
            }
        } else if (v == mBtSubmit) {
            EventBus.getDefault().post("RegisterCardActivity");
        }
    }

    @Override
    public void onBackPressed() {
        if (mBackStack.size() > 0) {
            popFragment();
        } else if(mBackStack.size()==0){
            EventBus.getDefault().post("back");
        }else {
            finishActivity();
        }
    }

    // 入栈
    public void pushFragment(Fragment fg) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction trans = fm.beginTransaction();
        trans.add(R.id.container, fg);
        trans.addToBackStack("registerBackStack");
        trans.commitAllowingStateLoss();
        mBackStack.push(fg);
    }

    // 出栈
    public void popFragment() {
        FragmentManager fm = getSupportFragmentManager();
        fm.popBackStackImmediate();
        Fragment fg = mBackStack.pop();
        FragmentTransaction trans = fm.beginTransaction();
        trans.remove(fg);
        trans.commitAllowingStateLoss();

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent it = new Intent("title");
                sendBroadcast(it);
            }
        }, 300);
    }

    // 查看栈顶fragment
    public Fragment peekFragment() {
        if (mBackStack.size() > 0) {
            return mBackStack.peek();
        }

        return null;
    }

    @Override
    public void setTitle(CharSequence text) {
        mFgTitleText.setText(text);
    }

    @Override
    public void setTitle(int resource) {
        mFgTitleText.setText(resource);
    }


    public void setBnt1ReadyColor(){
        mBtSubmit.setNormalBackgroundColor(getResources().getColor(R.color.blue_cc337cff));
        mBtSubmit.setNormalTextColor(getResources().getColor(R.color.common_color_c3_ffffff));
        mBtSubmit.setPressedBackgroundColor(getResources().getColor(R.color.common_color_c1cur_2e6fe6));
        mBtSubmit.setPressedTextColor(getResources().getColor(R.color.common_color_c3_ffffff));
    }

    public void setBnt1UnReadyColor(){
        mBtSubmit.setNormalBackgroundColor(getResources().getColor(R.color.common_color_c8_d5d7dc));
        mBtSubmit.setNormalTextColor(getResources().getColor(R.color.common_color_c3_ffffff));
        mBtSubmit.setPressedBackgroundColor(getResources().getColor(R.color.common_color_c8_d5d7dc));
        mBtSubmit.setPressedTextColor(getResources().getColor(R.color.common_color_c3_ffffff));
    }

    private boolean isShow = true;

    public void hideShowTitle(boolean b) {
        if (isShow && b) {
            mRlTitle.setVisibility(View.INVISIBLE);
            mBtSubmit.setVisibility(View.VISIBLE);
            isShow = !isShow;
        } else {
            mRlTitle.setVisibility(View.VISIBLE);
            mBtSubmit.setVisibility(View.INVISIBLE);
            isShow = !isShow;
        }

    }

    public void restoreTitle(){
        mRlTitle.setVisibility(View.VISIBLE);
        mBtSubmit.setVisibility(View.INVISIBLE);
        isShow = true;
    }
}
